<?php


namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use Dingo\Api\Exception\ResourceException;
use Dingo\Api\Routing\Helpers;

class BaseController extends Controller
{
    use Helpers;

    protected function errorBadRequest($validator)
    {
        $result   = [];
        $messages = $validator->errors()->toArray();
        $errMsg   = [];

        if ($messages) {
            foreach ($messages as $field => $errors) {
                foreach ($errors as $error) {
                    $result[] = [
                        'field' => $field,
                        'code'  => $error,
                    ];

                    $errMsg[] = $error;
                }
            }
        }

        if (!empty($errMsg)) {
            $errMsg = implode(",", $errMsg);
        }

        //房间
        $errMsg = "输入有误，请重试或联系管理员。";

        throw new ResourceException($errMsg, $result);
    }

}
